var username = null;
var password = null;
var isVerifyCodeCorrect = 0; //判断验证码是否正确，初始为0,正确为2，错误为1
var request = null;

//判断用户名是否为空
function checkUsername(){
	username = document.getElementById("username").value;
	if (username == "" || username == null){
		document.getElementById("checkUsername").innerHTML = "用户名不能为空";
		return false;
	}
	document.getElementById("checkUsername").innerHTML = "";
	return true;
}

//判断密码是否为空
function checkPassword(){
	password = document.getElementById("password").value;
	if (password == "" || password == null){
		document.getElementById("checkPassword").innerHTML = "密码不能为空";
		return false;
	}
	document.getElementById("checkPassword").innerHTML = "";
	return true;
}

//判断登录是否成功
function check(){
	if (!checkUsername() || !checkPassword()){
		return false;
	}
	if (isVerifyCodeCorrect != "2"){
		return false;
	}
	return true;
}

//判断验证码是否正确
function checkVerifyCode(){
	createXMLHttpRequest();
	
	verifyCodeInput = document.getElementById("code").value;
	var postContent = "verifyCodeInput=" + verifyCodeInput;
	
	request.open("POST", "checkVerifyCode", true);
	request.onreadystatechange = callback;
	request.setRequestHeader("Content-Type",
	"application/x-www-form-urlencoded");
	request.send(postContent);
	

	return true;
}

function callback(){
	if (request.readyState == 4){
		if (request.status == 200){
			if (request.responseText == "1"){
				//1代表验证码不正确
				document.getElementById("checkVerifyCode").innerHTML = "验证码错误";
				isVerifyCodeCorrect = 1;
			} else {
				isVerifyCodeCorrect = 2;
				document.getElementById("checkVerifyCode").innerHTML = "";
			}
		}
	}
}

//当点击验证码之后，重新生成验证码
function reloadVerifyImage(){
	var time = new Date().getTime();//防止浏览器缓存
	document.getElementById("verifyImage").src = "userVerifyImage?t=" + time;
}

function createXMLHttpRequest() {
	if (window.XMLHttpRequest) {
		// Mozilla
		request = new XMLHttpRequest();
	} else if (window.ActionXObject) {
		// IE
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
			}
		}
	}
}

